$(document).ready(function() {
	//用户名验证
	$("#username").blur(function() {
		var username = $(this).val();
		$.ajax({
			url: "verifyUsername.do",
			data: 'username=' + username,
			success: function(data) {
				if(username != ""){
					$("label").remove("#usernameError");
					if (data != "success") {
						if ($("#notNullUsernameError").length == 0) {
							$("#username").after("<label class='error' id='notNullUsernameError'>该用户名已被使用!</label>");
						}
						$("#submit").attr("disabled", true);
					} else {
						$("label").remove("#notNullUsernameError");
						$("#submit").attr("disabled", false);
					}
				}else{
					$("label").remove("#notNullUsernameError");
					if ($("#usernameError").length == 0) {
					    $("#username").after("<label class='error' id='usernameError'>用户名不能为空!</label>");
					}
					$("#submit").attr("disabled", true);
				}
				
			}
		})
	});

	//邮箱验证	
	$("#email").blur(function() {
		var email = $("#email").val();
		var checkemail=/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
		$.ajax({
			url: 'verifyEmail.do',
			data: "email=" + email,
			success: function(data) {
				if(email != ""){
					$("label").remove("#emailError");
					if (data != "success") {
						if ($("#notNullEmailError").length == 0) {
							$("#email").after("<label class='error' id='notNullEmailError'>该邮箱已被使用!</label>");
						}
						$("#submit").attr("disabled", true);
					} else {
						$("label").remove("#notNullEmailError");
						if(checkemail.test(email)==false){
							if ($("#checkEmailError").length == 0) {
							$("#email").after("<label class='error' id='checkEmailError'>邮箱格式有误!</label>");
						}
						$("#submit").attr("disabled", true);
						}else{
						    $("label").remove("#emailError");
						    $("label").remove("#checkEmailError");
						    $("#submit").attr("disabled", false);
						}
						
					}
				}else{
					$("label").remove("#notNullEmailError");
					if ($("#emailError").length == 0) {
					    $("#email").after("<label class='error' id='emailError'>邮箱不能为空!</label>");
					}
					$("#submit").attr("disabled", true);
				}
			}
		})
	});
	//确认密码验证
	$("#confirm_password").blur(function() {
		var password_val = $("#password").val();
		var confirm_password = $(this).val();
		if (confirm_password != "") {
			$("label").remove("#confirm_passwordError");
			if (confirm_password != password_val) {
				if ($("#notNullConfirm_passwordError").length == 0) {
					$("#confirm_password").after("<label class='error' id='notNullConfirm_passwordError'>两次密码不一致!</label>");
				}
				$("#submit").attr("disabled", true);
			} else {
				$("label").remove("#notNullConfirm_passwordError");
				$("#submit").attr("disabled", false);
			}
		} else {
			$("label").remove("#notNullConfirm_passwordError");
			if ($("#confirm_passwordError").length == 0) {
				$("#confirm_password").after("<label class='error' id='confirm_passwordError'>密码不能为空!</label>");
			}
			$("#submit").attr("disabled", true);
		}
	});
	//密码验证
	$("#password").blur(function() {
		var password_val = $("#password").val().length;
		if (password_val != "") {
			$("label").remove("#passwordError");
			$("label").remove("#notNullPasswordError");
			if (password_val < 6 || password_val > 12) {
				if ($("#notNullPasswordError").length == 0) {
					$("#password").after("<label class='error' id='notNullPasswordError'>密码在6到12位之间!</label>");
				}
				$("#submit").attr("disabled", true);
			}else{
				$("#submit").attr("disabled", false);
			}
		} else {
			$("label").remove("#notNullPasswordError");
			if ($("#passwordError").length == 0) {
				$("#password").after("<label class='error' id='passwordError'>密码不能为空!</label>");
			}
			$("#submit").attr("disabled", true);
		}
	});
	//联系号码验证
	$("#phone").blur(function() {
		var mobile = /(13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7}/;
		var tel = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
		var phoneVal = $("#phone").val();
		if (phoneVal != "") {
			$("label").remove("#phoneError");
			if (mobile.test(phoneVal) == false && tel.test(phoneVal) == false) {
				if ($("#notNullPhoneError").length == 0) {
					$("#phone").after("<label class='error' id='notNullPhoneError'>号码格式错误!</label>");
				}
				$("#submit").attr("disabled", true);
			}
			if (mobile.test(phoneVal) || tel.test(phoneVal)) {
				$("label").remove("#notNullPhoneError");
				$("#phone").attr("disabled", false);
			}
			if ($("#phoneError").length != 0) {
				$("label").remove("#phoneError");
				$("#phone").attr("disabled", false);
			}
		} else {
			if ($("#phoneError").length == 0) {
				$("#phone").after("<label class='error' id='phoneError'>号码不能为空!</label>");
			}
			$("#submit").attr("disabled", true);
		}



	});
	
	$("#submit").click(function() {
		var username = $("#username").val();
		var password = $("#password").val();
		var role = $("#role").val();
		var email = $("#email").val();
		var phone = $("#phone").val();
		var user =  'username=' + username + '&password=' + password + '&role=' + role + '&email=' + email + '&phone=' + phone;
		
		$.ajax({
			url: 'register.do',
			data: user,
			success: function(data) {
				alert(data.obj);
			}
		})
	});
});